<?php
require_once("tools.php");
//实例化表格，参数 int ： 最大行数
$house = new swoole_table(1024);
//设置表格字段  参数 （字段名：string ， 字段类型：int、float、string ， 长度：int）
$house->column('housekey', $house::TYPE_INT, 8);
$house->column('fd', $house::TYPE_INT, 8);
$house->column('name', $house::TYPE_STRING, 64);
$house->column('values', $house::TYPE_STRING, 128);
//$house->column('price',$house::TYPE_INT,11);
//创建表格
$house->create();
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->house = $house;

//todo 账号和fd的对应
//如果是微信可能是weixinopenid 和fd 对应


//step1 html 登录后new websocket + token
//step2 fd 和 token拿到了
//
//怎么处理掉线。 1 => a123
//             2 => b456
//             
//             



$server->on('open', function ($server, $req) {
    $tool = new Tool();
    echo 'open begin';
    echo "SWOOLE " . SWOOLE_VERSION . " 服务已启动" . PHP_EOL;
    $token = $req->get['token'];
    $fd = $req->fd;
    $weixin_arr = array();
    $weixin_arr['wid'] = 1;
    $weixin_arr['url'] = 'http';
    $str_arr = json_encode($weixin_arr);
    //连接的信息
    $house_arr = $tool->group3(6, $fd);
    $housekey = $house_arr['id'];
    if ($server->house->get($housekey)) {
        echo '第二次b';
        $rows = $server->house->get($housekey);
        var_dump($rows);
        $str_values = $rows['values'];
        $arr_values = json_decode($str_values, true);
        $values = $arr_values;
    } else {
        $values = array();
    }
    array_push($values, $fd);

    $str_arrBig = json_encode($values);
    $server->house->set($housekey, ['fd' => $fd, 'housekey' => $housekey, "values" => $str_arrBig]);

    echo "打开里的：";
    $rows = $server->house->get($housekey);
    var_dump($rows);

    $house = $server->house->get($housekey);

    $message = $tool->open_message_team($fd, $house_arr);
    
    $server->push($fd, $message);
    
    if ($house_arr['last'] == true) {
        //广播
        //分team 
        $rows = $server->house->get($housekey);
        $str_values = $rows['values'];
        $arr_values = json_decode($str_values, true);
        $values = $arr_values;
        foreach($values as $key => $value){
            $server->push($value, "开始");
        }
    }

});


$server->on('message', function ($server, $frame) {
    $tool = new Tool();
    $house = $server->house;
    echo "received message: {$frame->data}\n";
    $fd = $frame->fd;
    $point = $frame->data;
    $efd = $tool->find_fd_more($fd,$point);
    echo 'efd:'.$efd;
    echo "全部立案".count($server->connections);
    $house_arr = $tool->group3(6, $fd);
    $housekey = $house_arr['id'];
    $rows = $server->house->get($housekey);
    $str_values = $rows['values'];
    $arr_values = json_decode($str_values, true);
    $values = $arr_values;
    //知道这个动作的数组
    echo "只要立案".count($values);
    foreach ($values as $vfd) {
        echo PHP_EOL;
        echo '此时vfd'.$vfd;
        $message1 = '你@了'.$efd;
        $message2 = $fd.'号@了'.$efd;
        $message3 = $fd.'号@了你';
        if($vfd == $fd){
            $server->push($vfd, $message1);        
        }else{
            if($vfd == $efd){
                $server->push($vfd,$message3);     
            }else{
                $server->push($vfd,$message2);   
            }
            
        }
    }


});

$server->on('close', function ($server, $fd) {
    $tool = new Tool();
    //echo 'fd:'.$fd;
    //todo 根据fd 删除对应关系
    echo "connection close: {$fd}\n";
    //更新。这是是删除fd
    $house_arr = $tool->group3(6, $fd);
    $housekey = $house_arr['id'];
    if ($server->house->get($housekey)) {
        $rows = $server->house->get($housekey);
        $str_values = $rows['values'];
        $arr_values = json_decode($str_values, true);
        $values = $arr_values;
        $d_arr = array($fd);
        $values = array_diff($values, $d_arr);
        $str_arrBig = json_encode($values);
        $server->house->set($housekey, ['fd' => $fd, 'housekey' => $housekey, "values" => $str_arrBig]);
        $rows = $server->house->get($housekey);
    }


});

$server->start();

///update (dt) {}